package com.xiaohe66.web.application.common;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.common.web.sec.annotation.NeedRoleAll;
import com.xiaohe66.web.application.common.bo.TagSaveBo;
import com.xiaohe66.web.application.common.convert.TagBoConverter;
import com.xiaohe66.web.application.common.result.TagResult;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.common.agg.Tag;
import com.xiaohe66.web.domain.common.repository.TagRepository;
import com.xiaohe66.web.domain.common.service.TagService;
import com.xiaohe66.web.domain.common.value.TagId;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import com.xiaohe66.web.domain.sys.sec.value.RoleName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2023.01.29 14:52
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class TagAppService {

    private final TagBoConverter boConverter;
    private final TagRepository tagRepository;
    private final TagService tagService;
    private final SecurityService securityService;

    @NeedRoleAll(RoleName.TASK_ROLE_VALUE)
    public List<TagResult> listByCurrentAccount() {

        AccountId currentAccountId = securityService.getCurrentAccountId();

        List<Tag> tags = tagService.listByCreateId(currentAccountId);

        return boConverter.toResult(tags);
    }

    @NeedRoleAll(RoleName.TASK_ROLE_VALUE)
    public Long save(TagSaveBo saveBo) {

        if (saveBo.getId() == null) {
            saveBo.setId(IdWorker.genId());
        }
        Tag tag = boConverter.toAgg(saveBo);
        // TODO : 未检查是否重复
        tagRepository.save(tag);

        return saveBo.getId();
    }

    @NeedRoleAll(RoleName.TASK_ROLE_VALUE)
    public TagResult getById(Long tagId) {

        Tag tag = tagRepository.getById(new TagId(tagId));

        return boConverter.toResult(tag);
    }
}
